<?php

require_once 'vn.base.php';

class VNSession extends VNBase{

	public function __Construct(){
		try{
			/*
			 * These settings must be initiated
			 * first before any other variables
			 * are set.
			 */
			parent::__Construct();
			$this->startSession();

			//if debugging is turned on, print the Session details
			if($this->VNcore['__LPDebug'] == "session"){

				print "<pre id='debug'>".__CLASS__.":".__LINE__."<br/>";var_dump($_SESSION);print "</pre>";

			}
			
			/*
			 * Set the rest of the required variables
			 */
			$this->uObj = new VNUser($_SESSION['uData']['uaccount']);

			if(!$this->uObj){

				throw new VNException(__CLASS__, "Could not initiate VNSession", 500);

			}

		}catch( VNException $e ){

			print $e;
			die();
		}

	}

	/***************************
	 *	Session handelling
	 ***************************/
	private function startSession(){

		session_start();
		$_SESSION['id'] = session_id();

		/**
		 * Set guest value to users not logged in, and update
		 * active guests table accordingly.
		 */
		if(!isset($_SESSION['uData']) || empty($_SESSION['uData'])){
			
			$_SESSION['uData']['uaccount'] = 'guest';
			$_SESSION['uData']['ulevel'] = '1';
				
		}

	}
	
	public function getSiteLocation(){
		
		return $this->VNcore['__LPSite'];
		
	}

	/**
	 * isAdmin - Returns true if currently logged in user is
	 * an administrator, false otherwise.
	 */
	public function isAdmin(){

		if($this->uObj->permissions() == '7'){

			return true;

		}

		return false;
	}
	
	/**
	 * loggedIn - Returns true if current user is logged in,
	 * false otherwise.
	 */
	public function loggedIn(){
		
		if($this->uObj->account() != 'guest'){
			
			return true;
			
		}
		
		return false;
	}

	/***************************
	 *	User handelling
	 ***************************/

	/*
	 * FUNCTION -		createAccount
	 *
	 * @params
	 * 		array(
	 * 			$username,		$password1
	 * 			$password2,		$firstName
	 * 			$lastName,		$dateofbirth
	 * 			$gender,		$business
	 * 			$phoneNo,		$mobileNo
	 * 			$address,		$city
	 * 			$postcode,		$ulevel=false
	 * 		);
	 *
	 * @return
	 * 		boolean
	 */
	public function createAccount($uArr){
		try{

			if(!isset($uArr)){

				throw new VNException("Unable to create account, missing data", 501,__CLASS__);

			}

			if(!$this->uObj->userExists($uArr['username'])){

				if($this->uObj->createUser($uArr)){

					return true;

				}else{

					throw new VNException("Unable to create account, creation error", 502,__CLASS__);

					return false;

				}

			}else{

				throw new VNException("Unable to create account, user already exists", 503,__CLASS__);

				return false;

			}

			throw new VNException("Unable to create account", 504,__CLASS__);

			return false;

		}catch (VNException $e){

			print $e;

		}

	}

	/*
	 * FUNCTION -		createAccount
	 *
	 * @params
	 * 		array(
	 * 			$username,		$password1
	 * 			$password2,		$firstName
	 * 			$lastName,		$dateofbirth
	 * 			$gender,		$business
	 * 			$phoneNo,		$mobileNo
	 * 			$address,		$city
	 * 			$postcode,		$ulevel=false
	 * 		);
	 *
	 * @return
	 * 		boolean
	 */
	public function updateAccount($uArr){
		try{

			if(!isset($uArr)){

				throw new VNException("Unable to update account, missing data", 501,__CLASS__);

			}

			if($this->uObj->userExists($uArr['username'])){

				if($this->uObj->updateUser($uArr)){

					return true;

				}else{

					throw new VNException("Unable to update account, update error", 502,__CLASS__);

					return false;

				}

			}else{

				throw new VNException("Unable to update account, user does not exist", 503,__CLASS__);

				return false;

			}

			throw new VNException("Unable to update account", 504,__CLASS__);

			return false;

		}catch (VNException $e){

			print $e;

		}

	}
	
	/*
	 * FUNCTION -		login
	 *
	 * @params
	 * 		$name - username
	 * 		$pass - password
	 *
	 * @return
	 * 		boolean
	 */
	public function login($name,$pass){
		try{

			if($this->uObj->checkCredentials($name,$pass)){

				header('Location: '.$this->VNcore['__LPSite']);
				return true;

			}else{

				throw new VNException("Login Failed, please check credentials", 506,__CLASS__);

			}

		}catch (VNException $e){

			$_SESSION['error']['login'] = $e;
			return false;

		}

	}

	/*
	 * FUNCTION -		logout
	 *
	 * @params
	 * 		none
	 *
	 * @return
	 * 		none
	 */
	public function logout(){

		/* Destroy the session */
		unset($_SESSION);
		session_destroy();

		/* Redirect user to login page */
		header("Location: ".$this->VNcore['__LPSite']);

	}

	public function retrieveUserObj(){
		return $this->uObj;
	}

	public function displayRedirect($alertText,$redirect=false){
		$string = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
				<html>
				<head>
					<title>Your Page Title</title>
					<meta http-equiv='refresh' content='0;url=" . SITE_LOCATION . SITE_DIR;
		if($redirect == "admin"){
			$string .= DIR_ADMIN."main.php";
		}
		$string .= "'>
				</head>
				<body>
				<script>alert('{$alertText}');</script>
				</body>
				</html>";

		print $string;
	}
}
?>
